

let xhr = new XMLHttpRequest()
xhr.onreadystatechange = function(){
    // 取消请求
    // if(xhr.readyState === 1){
    //   xhr.abort()
    //   xhr = null
    // }
    // 进行条件判断
    if( xhr.readyState === 4 &&  xhr.status >= 200 && xhr.status < 300 || xhr.status === 304 ){
        console.log(xhr.responseText)
        const responseHeader = xhr.getAllResponseHeaders() // 获取响应头所有信息
     }
}
let url = addURLParams('http://localhost:3000/postapi')
xhr.open('post',url,false)
// xhr.setRequestHeader("Content-Type","application/json")
// 添加请求头字段
xhr.send({a:1})
// get
function addURLParams(url , name , value){
    if(!name && !value ) return url
    url += url.indexOf('?') === -1 ? '?':'&'
    url += `${encodeURIComponent(name)}=${encodeURIComponent(value)}`
    return url
}

